package tr.edu.metu.is.mega.server.business;

import tr.edu.metu.is.mega.common.dto.UserCoursesViewDTO;
import tr.edu.metu.is.mega.common.dto.UserRole;
import tr.edu.metu.is.mega.server.core.AbstractDTOHelper;
import tr.edu.metu.is.mega.server.entity.Usercoursesview;

/**
 * Helper class to convert between DTO and entity.
 */
public class UserCoursesViewDTOHelper extends AbstractDTOHelper<UserCoursesViewDTO, Usercoursesview> {

    /**
     * {@inheritDoc}
     */
    @Override
    public UserCoursesViewDTO convertToDTO(Usercoursesview from) {
        UserCoursesViewDTO to = new UserCoursesViewDTO();
        to.setPkid(from.getPkid());
        to.setCourseid(from.getCourseid());
        to.setCode(from.getCode());
        to.setName(from.getName());
        to.setRole(UserRole.findByCode(from.getRole()));
        to.setSemester(from.getSemester());
        to.setUser(from.getUser());
        to.setUserid(from.getUserid());

        return to;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void convertToEntity(UserCoursesViewDTO from, Usercoursesview to) {
        to.setPkid(from.getPkid());
        to.setCourseid(from.getCourseid());
        to.setCode(from.getCode());
        to.setName(from.getName());
        if (from.getRole() != null) {
            to.setRole(from.getRole().getCode());
        }
        to.setSemester(from.getSemester());
        to.setUser(from.getUser());
        to.setUserid(from.getUserid());
    }

}
